<?php

namespace app\admin\model;

use think\Model;


class GoodsType extends Model
{

    

    

    // 表名
    protected $name = 'goods_type';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];

    public function typeParams(){
        return $this->belongsToMany('GoodsParams','goods_type_params','params_id','type_id');
    }

    public function typeSpec(){
        return $this->belongsToMany('GoodsSpec','goods_type_spec','spec_id','type_id');
    }




    public function getTypeValue($type_id = 0)
    {
        $typeInfo = $this
            ->with(['typeParams','typeSpec.specValue'])
            ->where([ 'id' => $type_id ])
            ->find();

        //return $typeInfo;
        if($typeInfo) {

            //处理 属性
            $rel = [];
            if($typeInfo['type_spec']) {
                foreach($typeInfo['type_spec'] as $key => $val) {
                    $rel[$key]['spec']       = $val;
                    $rel[$key]['spec_value'] = $val->specValue;
                }
            }

            //处理 参数
            $rel1 = [];
            if($typeInfo['type_params']) {
                foreach($typeInfo['type_params'] as $key => $val) {
                    $rel1[$key]                = $val;
                    $rel1[$key]['value']       = ($val['value'] != 'text') ? explode(' ',$val['value']) : $val['value'];
                }
            }

            $typeInfo['spec'] = $rel;
            $typeInfo['params'] = $rel1;
            $result['msg'] ='获取成功';
            $result['data']   = $typeInfo;
            $result['status'] = true;
        }
        return $result;
    }




}
